﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SF.WebClient.Controllers
{
    public class DemoController : BasicController
    {
        #region Datagrid行编辑 自动取值、计算合计

        public static List<DatagridExcelDataModel> DatagridEditDb1 = new List<DatagridExcelDataModel>();

        public static List<dynamic> DatagridEditDb2 = new List<dynamic> { 
            new { Code = "001", Name = "菜刀", StandardModel = "XT001", Remark = "A", Stocks = 18 }, 
            new { Code = "002", Name = "盘龙棍", StandardModel = "PL002", Remark = "B", Stocks = 28 }, 
            new { Code = "003", Name = "井中月", StandardModel = "JZY003", Remark = "C", Stocks = 38 }, 
            new { Code = "004", Name = "元神禁锢", StandardModel = "YSJG004", Remark = "D", Stocks = 48 }, 
            new { Code = "005", Name = "斩妖泣血", StandardModel = "ZYQX005", Remark = "E", Stocks = 8 } };

        public ViewResult DatagridEditDemo1(string id)
        {
            ViewBag.ID = id;

            var model = DatagridEditDb1.FirstOrDefault(f => f.ApplyID == id);
            if (model == null) { model = new DatagridExcelDataModel() { ApplyStaff = "ly001", ApplyStaffName = "落阳", ApplyDepartment = "scb002", ApplyDepartmentName = "市场部", ApplyDate = DateTime.Now }; }

            return View(model);
        }

        public JsonResult GetDatagridEditData1(string id)
        {
            var data = DatagridEditDb1.Where(w => w.ApplyID == id).FirstOrDefault();
            if (data != null)
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                result.Add("rows", data.rowModels);
                result.Add("footer", new List<object> { new { Code = "合计", ApplyCount = data.rowModels.Sum(s => s.ApplyCount) } });

                return Json(result);
            }
            else
            {
                return Json(new { rows = new List<object>(), footer = new List<object> { new { Code = "合计", ApplyCount = 0 } } });
            }
        }

        public JsonResult GetDatagridEditTheGoodData(string code)
        {
            var data = DatagridEditDb2.FirstOrDefault(f => f.Code == code);

            return data == null ? Json(new { }) : Json(data);
        }

        [ValidateInput(false)]
        public JsonResult DatagridEditSave1(DatagridExcelDataModel model)
        {
            model.rowModels = SF.Utilities.Serialization.DeserializeJsToArray<RowModel>(model.rows);

            if (string.IsNullOrWhiteSpace(model.ApplyID))
            {
                //都是新增
                model.ApplyID = SF.Utilities.GuidHelper.GetNewFormatGuid();
                var tempRows = model.rowModels.ToList();
                foreach (var item in tempRows)
                {
                    item.ID = item.ID.Replace("-", string.Empty);
                }
                model.rowModels = tempRows.AsEnumerable();
                DatagridEditDb1.Add(model);
            }
            else
            {
                var record = DatagridEditDb1.FirstOrDefault(f => f.ApplyID == model.ApplyID);
                if (record == null) { return Json(new { success = false }); }
                //更新，需判定row的cud类型
                var tempRows = model.rowModels.ToList();
                List<RowModel> newRows = new List<RowModel>();
                foreach (var item in tempRows)
                {
                    if (item.ID.Contains("-"))
                    {
                        //新增的
                        item.ID = item.ID.Replace("-", string.Empty);
                        newRows.Add(item);
                    }
                    else
                    {
                        //编辑或删除的
                        if (item.isDeleted)
                        {

                        }
                        else
                        {
                            newRows.Add(item);
                        }
                    }
                }
                record.rowModels = newRows.AsEnumerable();
            }

            return Json(new { success = true, newId = model.ApplyID });
        }

        public JsonResult DatagridEditDataClear()
        {
            DatagridEditDb1.Clear();

            return Json(new { }, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region Datagrid编辑，内部编辑器combobox显示text的处理

        public ViewResult DatagridEditDemo2()
        {
            return View();
        }

        #endregion

        #region Datagrid，3行表头

        public ViewResult DatagridComplexTitleDemo()
        {
            return View();
        }

        #endregion

        #region Datagrid全自定义配置的列信息，并包含编辑器配置信息和常规的CUD功能

        // dbColName 字段名
        // title 列名
        // order 列排序号
        // colDataType 数据类型，目前未完全使用到
        // canEdit 是否可编辑
        // colInput 编辑器类型
        //      可选值：textbox（文本框）、combobox（下拉框）、datebox（日期框）、numberbox（数字框）、integerbox（整数框）、yyyyMMbox（年月框）、yyyyMMddbox（年月日框）、my97box（年月日时间框）
        // multiple 是否多选
        // spareCol2 数据字典类型
        // mustFill 是否必填
        // hint 说明提示，目前未使用到
        // defaultValue 默认值
        // editViewW 宽度
        // Hidable 是否可隐藏
        // Sortable 是否可排序
        public static List<ColumnConfigModel> DatagridColumnsConfigDb = new List<ColumnConfigModel> { 
            //new ColumnConfigModel{ dbColName = "mainCol1", title = "英语成绩", order = 10, colDataType="", canEdit = true, colInput = "numberbox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "", editViewW = "80", Hidable = true },
            //new ColumnConfigModel{ dbColName = "mainCol2", title = "数学成绩", order = 20, colDataType="", canEdit = true, colInput = "numberbox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "", editViewW = "80", Hidable = true },
            //new ColumnConfigModel{ dbColName = "mainCol3", title = "性别", order = 30, colDataType="", canEdit = true, colInput = "textbox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "男", editViewW = "80", Hidable = true },
            //new ColumnConfigModel{ dbColName = "mainCol4", title = "姓名", order = 40, colDataType="", canEdit = true, colInput = "textbox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "", editViewW = "80", Hidable = true },
            //new ColumnConfigModel{ dbColName = "mainCol5", title = "年龄", order = 50, colDataType="", canEdit = true, colInput = "numberbox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "", editViewW = "80", Hidable = true },
            //new ColumnConfigModel{ dbColName = "mainCol6", title = "出生日期", order = 60, colDataType="", canEdit = true, colInput = "datebox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "1990-01-01", editViewW = "110", Hidable = true },

            new ColumnConfigModel{ dbColName = "Name", title = "名称", order = 100, colDataType="", canEdit = true, colInput = "textbox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "测试新的名称", editViewW = "100", Hidable = false },
            new ColumnConfigModel{ dbColName = "Code", title = "编号", order = 200, colDataType="", canEdit = true, colInput = "textbox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "01", editViewW = "70", Hidable = true },
            new ColumnConfigModel{ dbColName = "Amount", title = "数量", order = 600, colDataType="gather", canEdit = true, colInput = "integerbox", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "1", editViewW = "", Sortable = true, Hidable = true },
            new ColumnConfigModel{ dbColName = "Unit", title = "单位", order = 300, colDataType="", canEdit = true, colInput = "textbox", multiple = false, spareCol2 = "", mustFill = false, hint = "", defaultValue = "台", editViewW = "100" , Hidable = true},
            new ColumnConfigModel{ dbColName = "SPE", title = "规格", order = 500, colDataType="", canEdit = true, colInput = "combobox", multiple = true, spareCol2 = "type2", mustFill = true, hint = "", defaultValue = "55", editViewW = "140", Hidable = true },
            new ColumnConfigModel{ dbColName = "Type", title = "类型", order = 400, colDataType="", canEdit = true, colInput = "combobox", multiple = false, spareCol2 = "type1", mustFill = true, hint = "", defaultValue = "", editViewW = "140", Hidable = true }
            ,
            new ColumnConfigModel{ dbColName = "mainCol6", title = "出生日期", order = 6000, colDataType="", canEdit = true, colInput = "yyyyMMbox", multiple = false, spareCol2 = "", mustFill = false, hint = "", defaultValue = "", editViewW = "110", Hidable = true },
            new ColumnConfigModel{ dbColName = "mainCol66", title = "出生日期2", order = 6001, colDataType="", canEdit = true, colInput = "yyyyMMddbox", multiple = false, spareCol2 = "", mustFill = false, hint = "", defaultValue = "", editViewW = "110", Hidable = true },
            new ColumnConfigModel{ dbColName = "mainCol666", title = "出生日期3", order = 6002, colDataType="", canEdit = true, colInput = "my97box", multiple = false, spareCol2 = "", mustFill = true, hint = "", defaultValue = "", editViewW = "110", Hidable = true }
        };

        public static List<dynamic> DatagridColumnsComboboxDb = new List<dynamic> { 
            new { value = "选项1", text = "选项1", type = "type1" },
            new { value = "选项2", text = "选项2", type = "type1" },
            new { value = "选项3", text = "选项3", type = "type1" },
            new { value = "选项4", text = "选项4", type = "type1" },
            new { value = "选项5", text = "选项5", type = "type1" },


            new { value = "11", text = "选项11", type = "type2" },
            new { value = "22", text = "选项22", type = "type2" },
            new { value = "33", text = "选项33", type = "type2" },
            new { value = "44", text = "选项44", type = "type2" },
            new { value = "55", text = "选项55", type = "type2" }
        };

        public ViewResult DatagridEditDemo3()
        {
            return View();
        }

        public JsonResult GetCustomizeColumnsConfigData()
        {
            var data = DatagridColumnsConfigDb.OrderBy(o => o.order);

            return Json(data);
        }

        public JsonResult GetCustomizeComboboxData(string type)
        {
            var data = DatagridColumnsComboboxDb.Where(w => w.type == type);
            return Json(data);
        }

        public JsonResult GetDatagridEditData3(string gatherField)
        {
            return Json(new List<object>());
        }


        #endregion


        #region 排列显示多个window，关闭一个，后面自动进位

        public ViewResult PanelsOneByOneDemo(int count = 7)
        {
            ViewBag.WindowCount = count;

            return View();
        }

        #endregion


        #region Textbox 背景变色

        public ViewResult TextboxBackgroundDemo()
        {
            return View();
        }

        #endregion

        #region Textbox 自动聚焦，解决IE8下无法通过“focus()”方法进行聚焦的问题

        public ViewResult TextboxFocusDemo()
        {
            return View();
        }

        #endregion


        #region numberbox 修改字体大小

        public ViewResult NumberboxFontSizeDemo()
        {
            return View();
        }

        #endregion
    }

    public class DatagridExcelDataModel
    {
        public string ApplyID { get; set; }

        public string ApplyStaff { get; set; }

        public string ApplyStaffName { get; set; }

        public string ApplyDepartment { get; set; }

        public string ApplyDepartmentName { get; set; }

        public DateTime? ApplyDate { get; set; }

        public string rows { get; set; }

        public IEnumerable<RowModel> rowModels { get; set; }

        public string content { get; set; }
    }

    public class RowModel
    {
        public string ID { get; set; }

        public string Code { get; set; }

        public string Name { get; set; }

        public string StandardModel { get; set; }

        public int? ApplyCount { get; set; }

        public int? AuditCount { get; set; }

        public string Remark { get; set; }

        public int? Stocks { get; set; }

        public bool isDeleted { get; set; }
    }

    public class ColumnConfigModel
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string title { get; set; }

        /// <summary>
        /// 字段名称
        /// </summary>
        public string dbColName { get; set; }

        /// <summary>
        /// 序号
        /// </summary>
        public int order { get; set; }

        /// <summary>
        /// 数据类型
        /// 可选值：group（分组字段）、calc（计算字段）、gather（信息收集字段）
        /// </summary>
        public string colDataType { get; set; }

        #region 落阳新增属性

        /// <summary>
        /// 是否可隐藏
        /// </summary>
        public bool Hidable { get; set; }

        /// <summary>
        /// 是否可排序
        /// </summary>
        public bool Sortable { get; set; }

        #endregion

        /// <summary>
        /// 是否可编辑
        /// </summary>
        public bool canEdit { get; set; }

        /// <summary>
        /// 编辑器类型
        /// 可选值：textbox（文本框）、combobox（下拉框）、datebox（日期框）、numberbox（数字框）
        /// </summary>
        public string colInput { get; set; }

        /// <summary>
        /// 是否多选
        /// 当 <see cref="colInput"/> 值为 combobox 时，本属性的值才有意义
        /// </summary>
        public bool multiple { get; set; }

        /// <summary>
        /// 数据字典类型值
        /// 当 <see cref="colInput"/> 值为 combobox 时，本属性才有值
        /// </summary>
        public string spareCol2 { get; set; }

        /// <summary>
        /// 是否必填
        /// </summary>
        public bool mustFill { get; set; }

        /// <summary>
        /// 字段说明提示
        /// 理解成 prompt 即可
        /// </summary>
        public string hint { get; set; }

        /// <summary>
        /// 默认值
        /// </summary>
        public string defaultValue { get; set; }

        /// <summary>
        /// 编辑器宽度
        /// 事实上对datagrid而言，编辑器宽度会撑满列宽，因此只需设置列宽即可
        /// </summary>
        public string editViewW { get; set; }
    }
}